package demo1;

//所谓状态模式 State
/*
当一个对象的内在状态改变时允许改变其行为，这个对象看起来好像是改变了其类
 */

public class Main {
    public static void main(String[] args) {
        Context context = new Context(new ConcreteStateA());

        context.Request();
        context.Request();
        context.Request();
        context.Request();

    }
}

abstract class State{
    public abstract void Handle(Context context);
}

class ConcreteStateA extends State{
    @Override
    public void Handle(Context context) {
        System.out.println("现在是状态A");
     context.setState(new ConcreteStateB());
    }
}
class ConcreteStateB extends State{
    @Override
    public void Handle(Context context) {
        System.out.println("现在是状态B");
        context.setState(new ConcreteStateA());
    }
}

class Context{
    private State state;
    public Context(State state){
        this.state = state;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }
    public void Request(){
        state.Handle(this);
    }
}